package com.zzyl.nursing.controller;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DeviceReportDataVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备信息Controller
 * 
 * @author ZHD
 * @date 2025-08-21
 */
@Api(tags = "设备信息管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;


    /**
     * 查询产品列表
     * @param productKey 产品key
     * @return 产品信息列表
     */
    @ApiOperation("查询产品列表")
    @GetMapping("/queryProduct/{productKey}")
    public R<List<ServiceCapability>> queryProduct(@PathVariable("productKey") String productKey){
        return R.ok(deviceService.queryProduct(productKey));
    }
    /**
     * 修改设备信息和IOT设备信息
     * @param deviceDto 设备信息
     * @return 结果
     */
    @ApiOperation("修改设备信息和IOT设备信息")
    @PutMapping
    public AjaxResult updateDeviceAndIot(@RequestBody DeviceDto deviceDto){
        deviceService.updateDeviceAndIot(deviceDto);
            return success();
    }
    /**
     * 查询设备上报数据
     * @param iotId 设备ID
     * @return 设备上报数据
     */
    @ApiOperation("查询设备上报数据")
    @GetMapping("/queryServiceProperties/{iotId}")
     public R<List<DeviceReportDataVo>> queryServiceProperties(@PathVariable("iotId") String iotId){
        return R.ok(deviceService.queryServiceProperties(iotId));
    }

    /**
     * 根据设备ID查询设备详情
     * @param iotId 设备ID
     * @return 设备详情
     */
    @ApiOperation("根据设备ID查询设备详情")
    @GetMapping(value="/{iotId}")
    public R<DeviceDetailVo> findDeviceById(@ApiParam("设备ID") @PathVariable("iotId") String iotId){
        return R.ok(deviceService.findDeviceById(iotId));
    }
    /**
     * 注册设备
     * @return
     */
    @ApiOperation("注册设备")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto deviceDto){
        deviceService.register(deviceDto);
        return success();
    }
    /**
     *查询所有产品列表
     */
    @ApiOperation("查询所有产品列表")
    @GetMapping("/allProduct")
    public R<List<ProductVo>> allProduct(){
        List<ProductVo> productVo=deviceService.allProduct();
        return R.ok(productVo);
    }

    /**
     * 同步产品列表
     * @return
     */
    @ApiOperation("同步产品列表")
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList(){
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询设备列表
     */
    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo<Device> list(@ApiParam("设备查询条件") Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @ApiOperation("导出设备信息列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "设备信息查询条件") HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备信息数据");
    }

//    /**
//     * 获取设备信息详细信息
//     */
//    @ApiOperation("获取设备信息详细信息")
//    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
//    @GetMapping(value = "/{id}",params = "type=long")
//    public R<Device> getInfo(@ApiParam("设备信息ID") @PathVariable("id") Long id)
//    {
//        return R.ok(deviceService.selectDeviceById(id));
//    }

    /**
     * 新增设备信息
     */
    @ApiOperation("新增设备信息")
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("设备信息信息") @RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }

//    /**
//     * 修改设备信息
//     */
//    @ApiOperation("修改设备信息")
//    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
//    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@ApiParam("设备信息信息") @RequestBody Device device)
//    {
//        return toAjax(deviceService.updateDevice(device));
//    }

    /**
     * 删除设备信息
     */
    @ApiOperation("删除设备信息")
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{iotId}")
    public AjaxResult remove(@ApiParam("设备信息ID数组") @PathVariable String iotId)
    {
        return toAjax(deviceService.deleteDeviceById(iotId));
    }
}
